<?php

namespace Jobs\Daemon\Netease;

/**
 * @name 录制文件生成回调
 * Class CallFile
 * @package Jobs\Daemon\Netease
 */
class CallFile extends \S\Thread\Worker {
    protected $whileSleep = 0;

    public function process() {
        try {
            $data = (new \Dao\Queue\Netease())->popFile();
            if ($data) {
                //已存在的数据不在处理
                $info = (new \Data\Netease\Callback())->get($data['id']);
                if (!$info) {
                    //回调入库
                    (new \Data\Netease\Callback())->add($data);
                    $msg = json_decode($data['msg'], true);
                    $file_info_list = $msg['fileinfo'];
                    foreach ($file_info_list as $file_info) {
                        $file['channel_id'] = $file_info['channelid'];
                        $file['vid'] = $file_info['vid'];
                        $file['url'] = $file_info['url'];
                        $file['pieceindex'] = $file_info['pieceindex'];
                        $file['size'] = $file_info['size'];
                        $file['info'] = json_encode($file_info);
                        (new \Data\Netease\File())->add($data);
                    }
                }
            } else {
                sleep(5);
            }
        } catch (\Exception $e) {
            \S\Log\Logger::getInstance()->warning(array('错误码:' . $e->getCode(), '错误信息:' . $e->getMessage(), json_encode($data)));
        }
    }
}